﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Win104.Common
{
    public class Config104
    {
        // I帧称为信息帧，长度一定大于6个字节，被称作长帧，用于传输数据
        // S帧称为确认帧，长度只有6个字节，被称作短帧，用于确认接收的I帧
        // U帧称为控制帧，长度只有6个字节，也被称作短帧，用于控制启动、停止、测试

        /// <summary>
        /// 启动帧，主站发给从站
        /// 从站回复确认帧：68 04 0B 00 00 00
        /// </summary>
        public static string U_START = "68 04 07 00 00 00";

        /// <summary>
        /// 键值对存放结果，方便快速读取
        /// </summary>
        private static ConcurrentDictionary<int, double> HashValue = new System.Collections.Concurrent.ConcurrentDictionary<int, double>();

        /// <summary>
        /// 遥控数据
        /// </summary>
        public static ConcurrentDictionary<string, int> RemoteControlHash = new System.Collections.Concurrent.ConcurrentDictionary<string, int>();

        /// <summary>
        /// 遥调数据
        /// </summary>
        public static ConcurrentDictionary<string, double> RemoteAdjustHash = new System.Collections.Concurrent.ConcurrentDictionary<string, double>();

        /// <summary>
        /// 时间戳
        /// </summary>
        public static DateTime UpdateTime = DateTime.Now;

        public static void InsertHash(int address, double dv)
        {
            if (HashValue.ContainsKey(address))
                HashValue[address] = dv;
            else
                HashValue.TryAdd(address, dv);
            UpdateTime = DateTime.Now;
    }

        public static double GetHash(int address)
        {
            if (HashValue.ContainsKey(address))
                return HashValue[address];
            else
                return 0;
        }

        public static int GetHashCount()
        {
            return HashValue.Count;
        }

    }
}
